import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const moduleA = {
  state: {
    aText: 'this is moduleA text'
  },
  mutations: {},
  actions: {},
  getters: {}
}

export default new Vuex.Store({
  state: {
    text: 'this is from vuex',
    settings: {
      flag: false,
      username: 'Kenny',
      version: 10
    }
  },
  getters: {
    getTextAlerted: state => state.text.replace(/\s/g, '!')
  },
  mutations: {
    updateText(state, value) {
      // console.log('update')
      state.text = value
    },
    toggleFlag(state) {
      state.settings = {
        ...state.settings,
        flag: !state.settings.flag
      }
    },
    updateVersion(state, value) {
      state.settings = {
        ...state.settings,
        version: value || state.settings.version + 1
      }
    }
  },
  actions: {
    updateVersionAsync(context) {
      return new Promise(resolve => {
        setTimeout(() => {
          context.commit('updateVersion')
          resolve()
        }, 1000)
      })
    }
  },
  modules: {
    a: moduleA
  }
})
